using System.Collections.Generic;
using System.Web.Mvc;
using iHouse.Common.Const;
using iHouse.Infrastructure;
using iHouse.Model.Data;
using iHouse.Model.Repository.Interface;
using iHouse.Resource;
using iHouse.Web.Infrastructure;
using Microsoft.Practices.Unity;

namespace iHouse.Web.Controllers
{
    public class NoteController : BaseController
    {
        #region Properties
        [Dependency]
        public INoteRepository NoteRepository { get; set; }

        protected override CategoryType ControllerType
        {
            get { return CategoryType.Note; }
        }

        protected override int DefaultPageSize
        {
            get
            {
                return 6;
            }
        }

        protected override string HomeView
        {
            get
            {
                return "HomeList";
            }
        }
        #endregion

        #region Actions

        public ActionResult HomeList(int? category, int? page, [PaingCriteriaBinder(typeof(NoteSearchFilter))]PagingCriteria pagingCriteria)
        {
            PaginationList<Note> result;

            if (category.HasValue)
            {
                result = this.NoteRepository.FindEntitiesByCategory(category.Value, page.HasValue ? page.Value : 1, DefaultPageSize);
            }
            else
            {
                pagingCriteria.PageSize = DefaultPageSize;
                pagingCriteria.PageIndex = page.HasValue ? page.Value : 1;
                result = this.NoteRepository.FindEntities(pagingCriteria);                
            }

            ViewBag.TypeCategories = this.Categories;
            return View(result);
        }

        [Authorize]
        public ActionResult Create()
        {
            return View(ViewConst.NOTE_EDIT, new Note().ToNoteFormView(this.Categories, 0));
        }

        [HttpPost]
        [Authorize]
        [ValidateInput(false)]
        public ActionResult Create([NoteBinder]Note note)
        {
            if (ModelState.IsValid)
            {
                this.NoteRepository.CreateNote(note);

                return Content(ActionConst.PARENT_PAGE_RELOAD);
            }

            return View(note.ToNoteFormView(this.Categories, note.CategoryID));
        }

        [HttpGet]
        [Authorize]
        public ActionResult Edit(int noteID)
        {
            var note = this.NoteRepository.GetNoteByID(noteID);

            return View(note.ToNoteFormView(this.Categories, note.CategoryID));
        }

        [Authorize]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int noteID, [NoteBinder]Note note)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    this.NoteRepository.UpdateNote(noteID, note);

                    return Content(ActionConst.PARENT_PAGE_RELOAD);
                }

                return View(note.ToNoteFormView(this.Categories, note.CategoryID));
            }
            catch
            {
                return View(note.ToNoteFormView(this.Categories, note.CategoryID));
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult Delete(int? noteID)
        {
            if (noteID.GetValueOrDefault() > 0)
            {
                this.NoteRepository.DeleteNote(noteID.GetValueOrDefault());

                return Json(new { Status = "succeed" });
            }
            else
            {
                return View(ViewConst.ERROR, new { controller = "Note", action = "Delete", message = Resources.ErrorIDCanNotNull });
            }
        }
        #endregion
    }
}
